package com.youmu.maven.emall.service.impl;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.youmu.maven.emall.dao.PermissionDao;
import com.youmu.maven.emall.entity.Permission;
import com.youmu.maven.emall.service.IPermissionService;
import com.youmu.maven.emall.service.support.BaseService;

@Service
public class PermissionService extends BaseService implements
		IPermissionService {
	@Autowired
	private PermissionDao permissionDao;

	@Override
	public Page<Permission> queryPermission(Permission permission, int page,
			int rows) {
		PageHelper.startPage(page, rows);
		Map<String, Object> map = initParam(permission);
		return permissionDao.getPermissionsByPage(map);
	}

	@Override
	public Set<Permission> queryPermissionByRoleId(Long roleId) {
		Set<Permission> permissions = permissionDao
				.getPermissionsByRoleId(roleId);
		return permissions;
	}

	@Override
	public Set<Permission> queryPermissionByMenuId(Long menuId) {
		Set<Permission> permissions = permissionDao
				.getPermissionsByMenuId(menuId);
		return permissions;
	}

	@Override
	public void deletePermissionById(Long id) {
		if (id == null)
			throw new RuntimeException("Null id to delete");
		Permission permission = new Permission();
		permission.setId(id);
		permissionDao.deletePermission(initParam(permission));
	}

	@Override
	public Long insertPermission(Permission permission) {
		return permissionDao.insertPermission(initParam(permission));
	}

	@Override
	public void updatePermission(Permission permission) {
		if (permission.getId() == null)
			throw new RuntimeException("Null id to update");
		permissionDao.updatePermission(initParam(permission));
	}
}
